package com.songlanyun.modules.ad.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;
import com.songlanyun.modules.ad.dao.AdDao;
import com.songlanyun.modules.ad.entity.AdEntity;
import com.songlanyun.modules.ad.entity.AdLocationEntity;
import com.songlanyun.modules.ad.service.AdLocationService;
import com.songlanyun.modules.ad.service.AdService;
import com.songlanyun.modules.exception.AdException;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("yxtAdService")
public class AdServiceImpl extends ServiceImpl<AdDao, AdEntity> implements AdService {


    @Resource
    private AdLocationService adLocationService;

    /**
     * 列表
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        IPage<AdEntity> page = this.baseMapper.adPage(new Query<AdEntity>().getPage(params), params);
        return new PageUtils(page);
    }

    /**
     * 获取广告、广告位信息
     *
     * @param id
     * @return
     */
    @Override
    public AdEntity getInfo(Integer id) {
        //查询广告
        AdEntity ad = this.getById(id, true);

        //查询对应广告位
        AdLocationEntity adLocationEntity = adLocationService.getByMarkCode(ad.getMarkCode(), true);
        ad.setAdLocationName(adLocationEntity.getName());//设置广告位名称

        return ad;
    }

    /**
     * 查询商铺广告
     *
     * @param params
     * @param shopId
     * @return
     */
    @Override
    public List<AdEntity> getShopAd(Map<String, Object> params, Long shopId) {

        String modelType = MapUtil.getStr(params, "model_type");//广告类型

        List<AdEntity> list = this.list(
                new LambdaQueryWrapper<AdEntity>()
                        .eq(shopId != null, AdEntity::getShopId, shopId)
                        .eq(StringUtils.isNotBlank(modelType), AdEntity::getModelType, modelType)
                        .orderByDesc(AdEntity::getIsTop)
//                        .orderByDesc(AdEntity::getTopTime)
                        .orderByAsc(AdEntity::getSort)
                        .orderByDesc(AdEntity::getCreateTime)
        );
        return list;
    }

    /**
     * 根据商铺id和广告位标识码查询对应的广告
     *
     * @param shopId
     * @param markCode
     * @return
     */
    @Override
    public List<AdEntity> getShopAdByAdL(Long shopId, String markCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("shopId", shopId);
        map.put("markCode", markCode);
        return adList(map);
    }

    /**
     * @param id
     * @param isThrow 查询的对象为空时是否抛出异常
     * @return
     */
    @Override
    public AdEntity getById(Integer id, boolean isThrow) {

        AdEntity ad = this.baseMapper.selectById(id);
        if (ad == null) {
            if (isThrow) {
                throw new RRException(AdException.AD_NOT_EXIST);
            }
        }
        return ad;
    }

    @Override
    public int countByMarkCode(String markCode) {
        return this.baseMapper.selectCount(
                new LambdaQueryWrapper<AdEntity>()
                        .eq(AdEntity::getMarkCode, markCode)
        );
    }

    @Override
    public List<AdEntity> adList(Map<String, Object> params) {
        Long shopId = MapUtil.getLong(params, "shopId");
        String markCode = MapUtil.getStr(params, "markCode");

        List<AdEntity> list = this.list(
                new LambdaQueryWrapper<AdEntity>()
                        .eq(ObjectUtil.isNotNull(shopId), AdEntity::getShopId, shopId)
                        .eq(StringUtils.isNotBlank(markCode), AdEntity::getMarkCode, markCode)
                        .eq(AdEntity::getOnOff, true)//上线的广告
                        .orderByDesc(AdEntity::getIsTop)
//                        .orderByDesc(AdEntity::getTopTime)
                        .orderByAsc(AdEntity::getSort)
                        .orderByDesc(AdEntity::getCreateTime)
        );

        if (list != null && list.size() > 1) {
            //判断该广告位能轮播几个广告
            AdLocationEntity adL = adLocationService.getOne(
                    new LambdaQueryWrapper<AdLocationEntity>()
                            .eq(AdLocationEntity::getMarkCode, markCode)
                            .orderByDesc(AdLocationEntity::getCreateTime)
                            .last("limit 1")
            );
            if (adL == null) {
                throw new RRException(AdException.AD_LOCATION_NOT_EXIST);
            }
            //只能轮播一条广告
            if (adL.getAdLocationType().equals(0)) {
                List<AdEntity> listOne = new ArrayList<>();
                listOne.add(list.get(0));
                return listOne;
            }
        }

        return list;
    }


}
